﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Net;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using Water.Model;
using WaterApp.Common;

namespace WaterApp.Business
{
    public partial class DropDownList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
        private void BindGrid()
        {
            string url = WebapiUrl.weburl() + "GetAllDropdownlists";
            
            var httpClient = new HttpClient();
            var responseJson = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
            IList<DropdownList> mr = JsonConvert.DeserializeObject<IList<DropdownList>>(responseJson);
            dgdownlist.DataSource = mr;
            dgdownlist.DataBind();
           
        }
        #region 下拉列表维护
        protected void dgdownlist_ItemCommand(object sender, DataGridCommandEventArgs e)
        {
            switch (((LinkButton)e.CommandSource).CommandName)
            {
                case "Add":
                    try
                    {
                        string str1 = ((TextBox)(e.Item.FindControl("ftparent"))).Text;
                        string str2 = ((TextBox)(e.Item.FindControl("ftselid"))).Text;
                        string str3 = ((TextBox)(e.Item.FindControl("ftselvalue"))).Text;
                        string str4 = ((TextBox)(e.Item.FindControl("ftmemo"))).Text;
                        DropdownList dpl = new DropdownList()
                        {
                            SELParent = str1,
                            SELID = str2,
                            SELVALUE = str3,
                            Memo = str4
                        };
                        InsertIntoDB(dpl);
                        BindGrid();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    break;
                default:
                    break;
            }
        }
        protected void dgdownlist_EditCommand(object sender, DataGridCommandEventArgs e)
        {
            dgdownlist.EditItemIndex = e.Item.ItemIndex;
            BindGrid();
        }
        protected void dgdownlist_CancelCommand(object sender, DataGridCommandEventArgs e)
        {
            dgdownlist.EditItemIndex = -1;
            BindGrid();
        }
        protected void dgdownlist_DeleteCommand(object sender, DataGridCommandEventArgs e)
        {
            try
            {
                int sid = Convert.ToInt32(e.Item.Cells[0].Text);
                DeleteDownList(sid);
                dgdownlist.EditItemIndex = -1;
                BindGrid();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        protected void dgdownlist_UpdateCommand(object sender, DataGridCommandEventArgs e)
        {
            try
            {
                int sid = Convert.ToInt32(e.Item.Cells[0].Text);
                string str1 = ((TextBox)(e.Item.FindControl("etparent"))).Text;
                string str2 = ((TextBox)(e.Item.FindControl("etselid"))).Text;
                string str3 = ((TextBox)(e.Item.FindControl("etselvalue"))).Text;
                string str4 = ((TextBox)(e.Item.FindControl("etmemo"))).Text;
                DropdownList dpl = new DropdownList()
                {
                    SID = sid,
                    SELParent = str1,
                    SELID = str2,
                    SELVALUE = str3,
                    Memo = str4
                };
                UpdateDownList(dpl);
                dgdownlist.EditItemIndex = -1;
                BindGrid();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        protected void dgdownlist_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dgdownlist.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }
        #endregion

        private void InsertIntoDB(DropdownList pddl)
        {
            try
            {
                string url = WebapiUrl.weburl() + "AddDropdownlist";
                var requestJson = JsonConvert.SerializeObject(pddl);
                HttpContent httpContent = new StringContent(requestJson);

                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                var httpClient = new HttpClient();
                var responseJson = httpClient.PostAsync(url, httpContent).Result.Content.ReadAsStringAsync().Result;             
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private void UpdateDownList(DropdownList pddl)
        {
            try
            {
                string url = WebapiUrl.weburl() + "EditDropdownList";
                var requestJson = JsonConvert.SerializeObject(pddl);
                HttpContent httpContent = new StringContent(requestJson);

                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                var httpClient = new HttpClient();
                var responseJson = httpClient.PostAsync(url,httpContent).Result.Content.ReadAsStringAsync().Result; 
                
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private void DeleteDownList(int sid)
        {
            try
            {
                string url = WebapiUrl.weburl() + "DeleteDropdownList/"+sid.ToString();
                //var requestJson = JsonConvert.SerializeObject(pddl);
                //HttpContent httpContent = new StringContent(requestJson);

                //httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                var httpClient = new HttpClient();
                var responseJson = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result; 
                
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        
    }
}